<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <%-- 页头 --%>
    <title>更新管理员 - ReadJava教程</title>
    <%@include file="../common/head.jsp" %>
</head>
<body>
<div class="wrapper">

    <%-- 导航 --%>
    <%@include file="manage-header.jsp" %>

    <%-- 侧边栏 --%>
    <%@include file="manage-sidebar.jsp" %>

    <div class="main-panel">
        <div class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <div class="card-title">更新管理员</div>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>账户编号</label>
                                            <input type="text" name="userId" value="${user.userId}"
                                                   disabled class="form-control input-square videoId">
                                        </div>
                                        <div class="form-group">
                                            <label>注册时间</label>
                                            <input type="text" name="registerTime" readonly
                                                   value="${user.registerTime}"
                                                   class="form-control input-square registerTime">
                                        </div>
                                        <div class="form-group">
                                            <label>上次登录</label>
                                            <input type="text" name="loginTime" readonly
                                                   value="${user.loginTime}"
                                                   class="form-control input-square loginTime">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>登录名</label>
                                            <input type="text" name="userName" value="${user.userName}"
                                                   class="form-control input-square userName">
                                        </div>
                                        <div class="form-group">
                                            <label>电子邮箱</label>
                                            <input type="text" name="userEmail"
                                                   value="${user.userEmail}"
                                                   class="form-control input-square userEmail">
                                        </div>
                                        <div class="form-group">
                                            <label>联系电话</label>
                                            <input type="text" name="userPhone"
                                                   value="${user.userPhone}"
                                                   class="form-control input-square userPhone">
                                        </div>
                                        <div class="form-group text-center">
                                            <label class="form-radio-label">
                                                <input class="form-radio-input userState" type="radio"
                                                       name="userState" value="0">
                                                <span class="form-radio-sign">启用账户</span>
                                            </label>
                                            <label class="form-radio-label">
                                                <input class="form-radio-input userState" type="radio"
                                                       name="userState" value="1">
                                                <span class="form-radio-sign">禁用账户</span>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                                <div class="card-action text-center">
                                    <button type="button" class="btn btn-info btn-success" id="updateAdminBtn">
                                        更新管理员
                                    </button>
                                    <button type="button" class="btn btn-info btn-danger"
                                            onclick="window.location.href='/manage/admin/list'">
                                        返回管理
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<%-- js --%>
<%@include file="../common/script.jsp" %>

<script>
    $(function () {
        var userState = parseInt('${user.userState}');
        switch (userState) {
            case 0:
                $(".userState").eq(0).attr('checked', true);
                break;
            case 1:
                $(".userState").eq(1).attr('checked', true);
                break;
            default:
                alert("设置选中状态bug");
        }

        $("#updateVideoBtn").on("click", function () {
            var videoId = $(".videoId").val();
            var videoTypeId = $('.videoTypeId option:checked').val();
            var videoTitle = $(".videoTitle").val();
            var videoUrl = $(".videoUrl").val();
            var videoIcon = $(".videoIcon").val();
            var videoState = $('.videoState:checked').val();
            if (videoId != null && videoId.length != 0) {
                if (videoTypeId != null && videoTypeId.length != 0) {
                    if (videoTitle != null && videoTitle.length != 0) {
                        if (videoUrl != null && videoUrl.length != 0) {
                            if (videoIcon != null && videoIcon.length != 0) {
                                if (videoState != null && videoState.length != 0) {
                                    $.ajax({
                                        type: "POST",
                                        url: "/manage/video/update",
                                        data: {
                                            videoId: videoId,
                                            videoTypeId: videoTypeId,
                                            videoTitle: videoTitle,
                                            videoUrl: videoUrl,
                                            videoIcon: videoIcon,
                                            videoState: videoState
                                        },
                                        dataType: "json",
                                        success: function (data) {
                                            if (data.status == 200) {
                                                alert(data.message);
                                                var video = data.video;
                                                $(".videoId").val(user.videoId);
                                                $(".videoTypeId").val(user.videoTypeId);
                                                $(".videoTitle").val(user.videoTitle);
                                                $(".videoUrl").val(user.videoUrl);
                                                $(".videoIcon").val(user.videoIcon);
                                                switch (user.videoState) {
                                                    case 0:
                                                        $(".videoState").eq(0).attr('checked', true);
                                                        break;
                                                    case 1:
                                                        $(".videoState").eq(1).attr('checked', true);
                                                        break;
                                                    default:
                                                        alert("设置选中状态bug");
                                                }
                                            }
                                        },
                                        error: function () {
                                            alert("操作失败：Ajax更新错误");
                                        }
                                    });
                                } else {
                                    alert("操作失败：未设置视频状态");
                                }
                            } else {
                                alert("操作失败：视频图标不能为空");
                            }
                        } else {
                            alert("操作失败：视频地址不能为空");
                        }
                    } else {
                        alert("操作失败：视频标题不能为空");
                    }
                } else {
                    alert("操作失败：视频类型不能为空");
                }
            } else {
                alert("操作失败：未设置视频状态");
            }
        });
    });
</script>
</html>